/* Android-Michi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * Android-Michi is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Android-Michi; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */
package controller;

import com.chaski.michi.R;

import view.DisplayManager;
import model.HumanTicTacToe;

public class TwoPlayerMichi implements ButtonInterface {

	DisplayManager displayManager;
	HumanTicTacToe model = new HumanTicTacToe();
	int turn=0;
	char player;
	public static char playerX = 'X';
	public static char playerO = 'O';
	
	public TwoPlayerMichi(DisplayManager dm)
	{
		player=playerX;
		model.reset();
		displayManager = dm;
	}
	
	
	@Override
	public void btn00Event() {
		if (model.store(0, 0, player))
		{
			displayManager.display00(player);
			if (model.checkForWinner(player))
		 		handleWinner();

			 if (player==playerX)
				 player=playerO;
			 else
				 player=playerX;
		}
	 	else
	 		displayManager.displayMessage(R.string.funny_error_string);
	}

	public void btn10Event() {
		if (model.store(1, 0, player))
		{
			displayManager.display10(player);
			if (model.checkForWinner(player))
		 		handleWinner();

			 if (player==playerX)
				 player=playerO;
			 else
				 player=playerX;
		}
	 	else
	 		displayManager.displayMessage(R.string.funny_error_string);
	}

	public void btn20Event() {
		if (model.store(2, 0, player))
		{
			displayManager.display20(player);
			if (model.checkForWinner(player))
		 		handleWinner();

			 if (player==playerX)
				 player=playerO;
			 else
				 player=playerX;
		}
	 	else
	 		displayManager.displayMessage(R.string.funny_error_string);
	}
	
	
	public void btn01Event() {
		if (model.store(0, 1, player))
		{
			displayManager.display01(player);
			if (model.checkForWinner(player))
		 		handleWinner();

			 if (player==playerX)
				 player=playerO;
			 else
				 player=playerX;
		}
	 	else
	 		displayManager.displayMessage(R.string.funny_error_string);
	}
	public void btn11Event() {
		if (model.store(1, 1, player))
		{
			displayManager.display11(player);

			 if (player==playerX)
				 player=playerO;
			 else
				 player=playerX;
		}
	 	else
	 		displayManager.displayMessage(R.string.funny_error_string);
	}
	public void btn21Event() {
		if (model.store(2, 1, player))
		{
			displayManager.display21(player);
			if (model.checkForWinner(player))
		 		handleWinner();

			 if (player==playerX)
				 player=playerO;
			 else
				 player=playerX;
		}
	 	else
	 		displayManager.displayMessage(R.string.funny_error_string);
	}


	
	
	public void btn02Event() {
		if (model.store(0, 2, player))
		{
			displayManager.display02(player);
			if (model.checkForWinner(player))
		 		handleWinner();

			 if (player==playerX)
				 player=playerO;
			 else
				 player=playerX;
		}
	 	else
	 		displayManager.displayMessage(R.string.funny_error_string);
	}
	public void btn12Event() {
		if (model.store(1, 2, player))
		{
			displayManager.display12(player);
			if (model.checkForWinner(player))
		 		handleWinner();

			 if (player==playerX)
				 player=playerO;
			 else
				 player=playerX;
		}
	 	else
	 		displayManager.displayMessage(R.string.funny_error_string);
	}
	public void btn22Event() {
		if (model.store(2, 2, player))
		{
			displayManager.display22(player);
			if (model.checkForWinner(player))
		 		handleWinner();

			 if (player==playerX)
				 player=playerO;
			 else
				 player=playerX;
		}
	 	else
	 		displayManager.displayMessage(R.string.funny_error_string);
	}
	
	public void handleWinner() {
		if (player == playerX)
			displayManager.displayWinner(R.string.player_x_string);
		else
			displayManager.displayWinner(R.string.player_o_string);
	}
	
}
